﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Rizmar
{
    public partial class Titulo : UserControl
    {
        LancamentoForm parent;
        //ClienteForm clienteParent;
        TaxasForm taxaParent;

        public Titulo()
        {
            InitializeComponent();
        }

        public Titulo(LancamentoForm form)
        {
            parent = form;
            InitializeComponent();
        }

        public bool Completo()
        {
            if (codigoTextBox.Text == string.Empty )
                return false;
            if (bancoTextBox.Text == string.Empty)
                return false;
            if (calculoDesagioLabel.Text == string.Empty)
                return false;
            if (calculoDiasLabel.Text == string.Empty)
                return false;
            if (calculoLiquidoLabel.Text == string.Empty)
                return false;
            if (sacadoTextBox.Text == string.Empty)
                return false;
            if (valorTextBox.Text == string.Empty)
                return false;
            return true;
        }

        private void valorTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
            if (e.KeyChar == '.' && parent.titulos[parent.n_titulos - 1].valorTextBox.Text.Contains("."))
                e.Handled = true;
            if (parent.titulos[parent.n_titulos - 1].valorTextBox.Text.Contains(".") && (parent.titulos[parent.n_titulos - 1].valorTextBox.Text.Length - parent.titulos[parent.n_titulos - 1].valorTextBox.Text.IndexOf('.') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        private void desagioTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
            if (e.KeyChar == '.' && parent.titulos[parent.n_titulos - 1].calculoDesagioLabel.Text.Contains("."))
                e.Handled = true;
            if (parent.titulos[parent.n_titulos - 1].calculoDesagioLabel.Text.Contains(".") && (parent.titulos[parent.n_titulos - 1].calculoDesagioLabel.Text.Length - parent.titulos[parent.n_titulos - 1].calculoDesagioLabel.Text.IndexOf('.') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        private void liquidoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && e.KeyChar != '.')
                e.Handled = true;
            if (e.KeyChar == '.' && parent.titulos[parent.n_titulos - 1].calculoLiquidoLabel.Text.Contains("."))
                e.Handled = true;
            if (parent.titulos[parent.n_titulos - 1].calculoLiquidoLabel.Text.Contains(".") && (parent.titulos[parent.n_titulos - 1].calculoLiquidoLabel.Text.Length - parent.titulos[parent.n_titulos - 1].calculoLiquidoLabel.Text.IndexOf('.') >= 3) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        private void diasTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        private void dTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        private void valorTextBox_Enter(object sender, EventArgs e)
        {
            System.Windows.Forms.Clipboard.Clear();
        }

        private void bancoTextBox_Enter(object sender, EventArgs e)
        {
            System.Windows.Forms.Clipboard.Clear();
        }

        private void codigoTextBox_Enter(object sender, EventArgs e)
        {
            System.Windows.Forms.Clipboard.Clear();
        }

        private void desagioTextBox_Enter(object sender, EventArgs e)
        {
            System.Windows.Forms.Clipboard.Clear();
        }

        private void liquidoTextBox_Enter(object sender, EventArgs e)
        {
            System.Windows.Forms.Clipboard.Clear();
        }

        private void diasTextBox_Enter(object sender, EventArgs e)
        {
            System.Windows.Forms.Clipboard.Clear();
        }

        private void dTextBox_Enter(object sender, EventArgs e)
        {
            System.Windows.Forms.Clipboard.Clear();
        }

        private void pesquisaSacadoButton_Click(object sender, EventArgs e)
        {
            PesquisaSacadoForm form = new PesquisaSacadoForm(parent);
            form.ShowDialog();
        }

        private void tipoComboBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        private void bancoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        private void codigoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        private void sacadoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        private void dataBaseDateTimePicker_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        private void vencimentoDateTimePicker_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        public void setSacadoTextBox(string s) { sacadoTextBox.Text = s; }
        public void setSacadoLabel(string s) { codigoSacadoLabel.Text = s; }

        private void pesquisaSacadoButton_Click_1(object sender, EventArgs e)
        {
            PesquisaSacadoForm pesquisa = new PesquisaSacadoForm(this);
            pesquisa.ShowDialog();
        }

        /*public float getPisTextBox()
        {
            return float.Parse(pisTextBox.Text);
        }
        public float getIrpjTextBox()
        {
            return float.Parse(irpjTextBox.Text);
        }
        public float getIofTextBox()
        {
            return float.Parse(ioftextBox.Text);
        }
        public float getIssTextBox()
        {
            return float.Parse(issTextBox.Text);
        }*/

        public String getTextBoxBancoText()
        {
            return bancoTextBox.Text;
        }

        public string getTextBoxCodigoText()
        {
            return codigoTextBox.Text;
        }

        public DateTime getTextBoxVencimentoText()
        {
            return vencimentoDateTimePicker.Value;
        }

        public DateTime getTextBoxDataBaseText()
        {
            return dataBaseDateTimePicker.Value;
        }

        public float getTextBoxValorText() 
        {
            return float.Parse(valorTextBox.Text);
        }

        public int getlabelCalculoDiasText()
        {
            string selectString = "SELECT d FROM Cliente WHERE id = " + parent.getTextBoxCódigoText() + "";
            int dCliente;
               
            using (SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
               
                SqlCommand command =
                    new SqlCommand(selectString, cn);
                cn.Open();

                SqlDataReader reader = command.ExecuteReader();

                // Call Read before accessing data.
                while (reader.Read())
                {
                    dCliente = Convert.ToInt32(reader[0]);
                    int dMais = Convert.ToInt32(dTextBox.Text);
                    DateTime SomaDias;
                    SomaDias = vencimentoDateTimePicker.Value;
                    SomaDias = SomaDias.AddDays(dCliente + dMais);
                    if (SomaDias.DayOfWeek == DayOfWeek.Sunday)
                    {
                        SomaDias = SomaDias.AddDays(1);
                    }
                    else
                        if (SomaDias.DayOfWeek == DayOfWeek.Saturday)
                        {
                            SomaDias = SomaDias.AddDays(2);
                        }

                    TimeSpan diferenca = SomaDias.Subtract(dataBaseDateTimePicker.Value);
                    int dias = Convert.ToInt32(diferenca.Days);
                    dias = dias + 1;
                    calculoDiasLabel.Text = Convert.ToString(dias);
                }

                // Call Close when done reading.
                reader.Close();
            }

            return Convert.ToInt32(calculoDiasLabel.Text);
        }

        public float getLabelLiquidoText()
        {
            string selectString = "SELECT taxa , adm FROM Cliente WHERE id = " + parent.getTextBoxCódigoText() + "";
           
            double taxaMensal, taxaADM;

            using (SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                SqlCommand command =
                    new SqlCommand(selectString, cn);
                cn.Open();

                SqlDataReader reader = command.ExecuteReader();

                // Call Read before accessing data.
                while (reader.Read())
                {
                    string temp = Convert.ToString(reader[0]);
                    string temp1 = Convert.ToString(reader[1]);
                    calculoLiquidoLabel.Text = temp;
                    taxaMensal = double.Parse(temp);
                    calculoLiquidoLabel.Text = temp1;
                    taxaADM = double.Parse(temp1);

                    int dia = Convert.ToInt32(calculoDiasLabel.Text);
                    double jurosDia = ((taxaMensal * 0.01) / 30);
                    double des = (1 - taxaADM*0.01)*(double.Parse(valorTextBox.Text) * (double)Math.Pow((1 - jurosDia), dia));
                    calculoLiquidoLabel.Text = Convert.ToString(des);
                }

                // Call Close when done reading.
                reader.Close();
            }

            return float.Parse(calculoLiquidoLabel.Text);
        }

        public float getLabelDesagioText()
        {
            string selectString = "SELECT * FROM Taxa";

            float pis = 0, irpj = 0, iof = 0, iss = 0;

            using (SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                
                SqlCommand command =
                    new SqlCommand(selectString, cn);
                cn.Open();

                SqlDataReader reader = command.ExecuteReader();

                // Call Read before accessing data.
                while (reader.Read())
                {
                    string pisTemp = Convert.ToString(reader[0]);
                    string irpjTemp = Convert.ToString(reader[1]);
                    string iofTemp = Convert.ToString(reader[2]);
                    string issTemp = Convert.ToString(reader[3]);

                    pis = float.Parse(pisTemp);
                    irpj = float.Parse(irpjTemp);
                    iof = float.Parse(iofTemp);
                    iss = float.Parse(issTemp);
                  
                }

                // Call Close when done reading.
                reader.Close();
            }
            string selectString1 = "SELECT adm FROM Cliente WHERE id = " + parent.getTextBoxCódigoText() + "";
            float taxaAdm = 0;

            using (SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                
                SqlCommand command =
                    new SqlCommand(selectString, cn);
                cn.Open();

                SqlDataReader reader = command.ExecuteReader();

                // Call Read before accessing data.
                while (reader.Read())
                {
                    string tempTaxaAdm = Convert.ToString(reader[0]);
              
                }

                // Call Close when done reading.
                reader.Close();
            }
            float desag;
            float temp = getLabelLiquidoText();

            if (parent.getfactoringRadioButton() == true)
            {
                desag = float.Parse(valorTextBox.Text) - temp;
                desag = desag - desag * (pis / 100) - float.Parse(calculoDiasLabel.Text) * (iof / 100) - iss * taxaAdm;
                string td = Convert.ToString(desag);
                MessageBox.Show("Fac: " + td);
            }
            else
            {
                desag = float.Parse(valorTextBox.Text) - temp;
                string td = Convert.ToString(desag);
                MessageBox.Show("Serv: " + td);
            }

            calculoDesagioLabel.Text = Convert.ToString(desag);  
            return float.Parse(calculoDesagioLabel.Text);
        }

        public int getTextBoxDText()
        {
            if (dTextBox.TextLength == 0) dTextBox.Text = "0";
            return Convert.ToInt32(dTextBox.Text);
        }

        private void panel_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
